package minov.parser.utils;

import java.util.Vector;

/**
 *  Class with some auxiliary methods for processing strings.
 */
public class StringUtils {

   public static int STRING_NOT_FOUND = -1;

   public static boolean textFound(int foundResult) {
      if (foundResult == STRING_NOT_FOUND) {
         return false;
      }
      return true;
   }

   public static boolean isEmptyString(String value) {
      if (value == null || value.length() == 0)
         return true;
      return false;
   }

   public static String getStringFromArray(String[] stringArray, int index) {
      if (stringArray == null || index < 0)
         return "";
      if (stringArray.length > index) {
         return stringArray[index];
      }
      return "";
   }

   public static String createSeparatorsRegexp(String[] separators) {

      StringBuilder resultRegexp = new StringBuilder();
      for (String separator : separators) {

         if (resultRegexp.length() > 0) {
            resultRegexp.append("|");
         }

         resultRegexp.append(separator);
      }
      return resultRegexp.toString();
   }

   public static String createSeparatorsRegexpEnd(String[] separators) {

      String separatorRegexp = createSeparatorsRegexp(separators);
      return ".*(" + separatorRegexp + ")$";
   }

   public static boolean endsWithSeparator(String text, String endRegexp) {

      if (text.matches(endRegexp)) {
         return true;
      }
      return false;
   }
   private static String[] EMPTY_STRING_ARRAY = {};
   public static String[] wrapText(String text, int width) {
      Vector<String> result = new Vector<String>();

      // split at white spaces
      if (isEmptyString(text)) {
         return EMPTY_STRING_ARRAY;
      }
      String[] splittedText = text.split("\\s");
      StringBuilder line = new StringBuilder();
      for (String word : splittedText) {
         {
            if (line.length()+1+word.length() > width)
            {
              result.add(line.toString());
              line.setLength(0);
              line.append(word);
            }else
            {
               if (line.length()>0)
                  line.append(" ");
               line.append(word);
            }
         } 
      }
      if (line.length()>0)
      {
         result.add(line.toString());
      }

      return result.toArray(new String[0]);

   }
   
   public static String repeatCharacter(Character repeatedChar, int repetition)
   {
      StringBuilder result = new StringBuilder();
      int repetitionLeft= repetition;
      while (repetitionLeft > 0) {
         repetitionLeft--;
         result.append(repeatedChar);
      }
      return result.toString(); 
   }
   
   
}
